#include "test.h"

DAC_HandleTypeDef    DacHandle;
static DAC_ChannelConfTypeDef sConfig;
const uint8_t aEscalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF};
__IO uint8_t ubSelectedWavesForm = 1;

static void Error_Handler(void)
{
  /* Turn LED3 on */

  while(1)
  {
  }
}
static void DAC_Ch1_TriangleConfig(void)
{
  /*##-1- Initialize the DAC peripheral ######################################*/
  if (HAL_DAC_Init(&DacHandle) != HAL_OK)
  {
    /* DAC initialization Error */
    Error_Handler();
  }

  /*##-2- DAC channel2 Configuration #########################################*/
  sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;

  if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL) != HAL_OK)
  {
    /* Channel configuration Error */
    Error_Handler();
  }

  /*##-3- DAC channel2 Triangle Wave generation configuration ################*/
  if (HAL_DACEx_TriangleWaveGenerate(&DacHandle, DACx_CHANNEL, DAC_TRIANGLEAMPLITUDE_1023) != HAL_OK)
  {
    /* Triangle wave generation Error */
    Error_Handler();
  }

  /*##-4- Enable DAC Channel1 ################################################*/
  if (HAL_DAC_Start(&DacHandle, DACx_CHANNEL) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }

  /*##-5- Set DAC channel1 DHR12RD register ################################################*/
  if (HAL_DAC_SetValue(&DacHandle, DACx_CHANNEL, DAC_ALIGN_12B_R, 0x100) != HAL_OK)
  {
    /* Setting value Error */
    Error_Handler();
  }
}
static void DAC_Ch1_EscalatorConfig(void)
{
  /*##-1- Initialize the DAC peripheral ######################################*/
  if (HAL_DAC_Init(&DacHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /*##-1- DAC channel1 Configuration #########################################*/
  sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;

  if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DACx_CHANNEL) != HAL_OK)
  {
    /* Channel configuration Error */
    Error_Handler();
  }

  /*##-2- Enable DAC selected channel and associated DMA #############################*/
  if (HAL_DAC_Start_DMA(&DacHandle, DACx_CHANNEL, (uint32_t *)aEscalator8bit, 6, DAC_ALIGN_8B_R) != HAL_OK)
  {
    /* Start DMA Error */
    Error_Handler();
  }
}
void TIM6_Config(void)
{
  static TIM_HandleTypeDef  htim;
  TIM_MasterConfigTypeDef sMasterConfig;

  /*##-1- Configure the TIM peripheral #######################################*/
  /* Time base configuration */
  htim.Instance = TIM6;

  htim.Init.Period            = 0x7FF;
  htim.Init.Prescaler         = 0;
  htim.Init.ClockDivision     = 0;
  htim.Init.CounterMode       = TIM_COUNTERMODE_UP;
  htim.Init.RepetitionCounter = 0;
  htim.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  HAL_TIM_Base_Init(&htim);

  /* TIM6 TRGO selection */
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim, &sMasterConfig);

  /*##-2- Enable TIM peripheral counter ######################################*/
  HAL_TIM_Base_Start(&htim);
}
void test(void)
{
	DacHandle.Instance = DACx;
	TIM6_Config();
	HAL_DAC_DeInit(&DacHandle);
	while(1)
	{
		
		display_adc_info();
		DAC_Ch1_EscalatorConfig();
	}
}


//void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
//{
//  GPIO_InitTypeDef          GPIO_InitStruct;
//  static DMA_HandleTypeDef  hdma_dac1;

//  /*##-1- Enable peripherals and GPIO Clocks #################################*/
//  /* Enable GPIO clock ****************************************/
//  DACx_CHANNEL_GPIO_CLK_ENABLE();
//  /* DAC Periph clock enable */
//  DACx_CLK_ENABLE();
//  /* DMA1 clock enable */
//  DMAx_CLK_ENABLE();

//  /*##-2- Configure peripheral GPIO ##########################################*/
//  /* DAC Channel1 GPIO pin configuration */
//  GPIO_InitStruct.Pin = DACx_CHANNEL_PIN;
//  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
//  GPIO_InitStruct.Pull = GPIO_NOPULL;
//  HAL_GPIO_Init(DACx_CHANNEL_GPIO_PORT, &GPIO_InitStruct);

//  /*##-3- Configure the DMA ##########################################*/
//  /* Set the parameters to be configured for DACx_DMA_STREAM */
//  hdma_dac1.Instance = DACx_DMA_INSTANCE;


//  hdma_dac1.Init.Direction = DMA_MEMORY_TO_PERIPH;
//  hdma_dac1.Init.PeriphInc = DMA_PINC_DISABLE;
//  hdma_dac1.Init.MemInc = DMA_MINC_ENABLE;
//  hdma_dac1.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
//  hdma_dac1.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
//  hdma_dac1.Init.Mode = DMA_CIRCULAR;
//  hdma_dac1.Init.Priority = DMA_PRIORITY_HIGH;

//  HAL_DMA_Init(&hdma_dac1);

//  /* Associate the initialized DMA handle to the the DAC handle */
//  __HAL_LINKDMA(hdac, DMA_Handle1, hdma_dac1);

//  /*##-4- Configure the NVIC for DMA #########################################*/
//  /* Enable the DMA2_Channel3 IRQ Channel */
//  HAL_NVIC_SetPriority(DACx_DMA_IRQn, 2, 0);
//  HAL_NVIC_EnableIRQ(DACx_DMA_IRQn);

//}

/**
  * @brief  DeInitializes the DAC MSP.
  * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
  *         the configuration information for the specified DAC.
  * @retval None
  */
//void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
//{
//  /*##-1- Reset peripherals ##################################################*/
//  DACx_FORCE_RESET();
//  DACx_RELEASE_RESET();

//  /*##-2- Disable peripherals and GPIO Clocks ################################*/
//  /* De-initialize the DAC Channel1 GPIO pin */
//  HAL_GPIO_DeInit(DACx_CHANNEL_GPIO_PORT, GPIO_PIN_5);

//  /*##-3- Disable the DMA Stream ############################################*/
//  /* De-Initialize the DMA Stream associate to DAC_Channel1 */
//  HAL_DMA_DeInit(hdac->DMA_Handle2);

//  /*##-4- Disable the NVIC for DMA ###########################################*/
//  HAL_NVIC_DisableIRQ(DACx_DMA_IRQn);
//}

//void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
//{
//  /* TIM6 Periph clock enable */
//  __HAL_RCC_TIM6_CLK_ENABLE();
//}

//void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
//{

//  /*##-1- Reset peripherals ##################################################*/
//  __HAL_RCC_TIM6_FORCE_RESET();
//  __HAL_RCC_TIM6_RELEASE_RESET();
//}
